package com.peng.cms.controller.api;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.peng.cms.entity.CrmBanner;

import com.peng.cms.entity.vo.BannerQuery;
import com.peng.cms.service.CrmBannerService;
import com.peng.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author Dengp
 * @since 2022-06-18
 */
@RestController
@RequestMapping("/api/cms/bannerAdmin")
@CrossOrigin
public class CrmBannerAdminController {
    @Autowired
    private CrmBannerService crmBannerService;

    //条件分页查询banner
    @PostMapping("/pageBanner/{page}/{limit}")
    public Result pageBanner(@PathVariable Integer page,
                             @PathVariable Integer limit,
                             @RequestBody(required = false) BannerQuery bannerQuery){
        Page<CrmBanner> bannerPage = new Page<>(page,limit);
        LambdaQueryWrapper<CrmBanner> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(bannerQuery.getName()!=null, CrmBanner::getTitle,bannerQuery.getName());
        queryWrapper.ge(bannerQuery.getBegin()!=null, CrmBanner::getGmtCreate,bannerQuery.getBegin());
        queryWrapper.le(bannerQuery.getEnd()!=null, CrmBanner::getGmtModified,bannerQuery.getEnd());
        crmBannerService.page(bannerPage,queryWrapper);
        return Result.ok().data(bannerPage);
    }

    //添加banner
    @PostMapping("/addBanner")
    @CacheEvict(value = "banner",allEntries = true)
    public Result addBanner(@RequestBody CrmBanner crmBanner){
        boolean flag = crmBannerService.save(crmBanner);
        return flag? Result.ok(): Result.error();
    }

    //修改banner
    @PutMapping("/updateBanner")
    @CacheEvict(value = "banner",allEntries = true)
    public Result updateBanner(@RequestBody CrmBanner crmBanner){
        boolean flag = crmBannerService.updateById(crmBanner);
        return flag? Result.ok(): Result.error();
    }

    //修改banner
    @CacheEvict(value = "banner",allEntries = true)
    @DeleteMapping("/deleteBanner/{id}")
    public Result deleteBanner(@PathVariable String id){
        crmBannerService.removeById(id);
        return Result.ok();
    }

    //修改banner
    @GetMapping("/getBanner/{id}")
    public Result getBanner(@PathVariable String id){
        CrmBanner banner = crmBannerService.getById(id);
        return Result.ok().data(banner);
    }

}

